
#ifndef __HITTABLE_LIST_H__
#define __HITTABLE_LIST_H__

#include <memory>
#include <vector>
#include "hittable.hh"

using std::shared_ptr;
using std::make_shared;

class hittable_list : public hittable {
	public : 
		std::vector<shared_ptr<hittable>> objs;

	public : 
		hittable_list() {}
		hittable_list(shared_ptr<hittable> obj) { add(obj); }

		void clear() { objs.clear(); }
		void add(shared_ptr<hittable> obj) { objs.push_back(obj); }
		
		virtual bool hit(const ray& r, double min_t, double max_t, hit_record& rec) const override;		
};

bool hittable_list::hit(const ray& r, double min_t, double max_t, hit_record& rec) const {
	hit_record temp_record;
	bool hit_anything = false;
	auto closest_so_far = max_t;

	for (const auto& obj : objs) {
		if (obj->hit(r, min_t, closest_so_far, temp_record)) {
			hit_anything = true;
			closest_so_far = temp_record.t;
			rec = temp_record;	
		}	
	}

	return hit_anything;
}

#endif
